﻿using _9jCardRegisterCollection.Utils;
using AntdUI;
using CardRegisterCollection.Model;
using CardRegisterCollection.Utility.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9jCardRegisterCollection.Views
{
    public partial class CarEdit : UserControl
    {
        public CarEdit(AntdUI.Window _window, CardRegisterCollection.Model.Car _car )
        {       
            InitializeComponent();
            this.car = _car;
            window = _window;
            txtWorkUnit.Text = car.UseUnit;
            GetCardType();
            cmbCardType.SelectedValue = car.CardType;
            txtGender.Text = car.DriverName;
            txtIdNum.Text = car.DriverIDNumber;
            txtName.Text = car.CarNumber;
            txtTelephone.Text = car.Telephone;
            txtRemark.Text = car.Remark;  
        }
        public bool SureToChange = false;
        private AntdUI.Window window;
        
        private CardRegisterCollection.Model.Car car = new CardRegisterCollection.Model.Car();
        CardRegisterCollection.BLL.CardType cardTypeHelper = new CardRegisterCollection.BLL.CardType();
        public bool Submit { set; get; }

        private void button_ok_Click(object sender, EventArgs e)
        {
            SureToChange = false;
            if(Validation.CheckEmpty(window, this.txtName, "请输入车牌号") && Validation.CheckEmpty(window, this.txtGender, "请输入驾驶员姓名") && Validation.CheckEmpty(window, this.txtIdNum, "请输入驾驶员身份证号") && Validation.CheckEmpty(window, this.txtWorkUnit, "请输入使用单位") && Validation.CheckEmpty(window, this.cmbCardType, "请输入办证类型"))
            {
              
                car.CarNumber = this.txtName.Text;
                car.DriverName = this.txtGender.Text;
                car.DriverIDNumber = this.txtIdNum.Text;
                car.CardType = this.cmbCardType.Text.ToString();
                car.UseUnit = this.txtWorkUnit.Text;
                car.Telephone = this.txtTelephone.Text;
                car.Remark = this.txtRemark.Text;
                //新增
                if (string.IsNullOrEmpty(car.ID))
                { 
                    car.ID = Guid.NewGuid().ToString();
                    car.CreatedBy = CurrentLoginInfo.UserInfo.UserName;
                    car.CreatedAt = DateTime.Now;
                    
                }
                else
                {
                    car.LastUpdatedBy = CurrentLoginInfo.UserInfo.UserName;
                    car.LastUpdatedAt = DateTime.Now;
                }
                SureToChange = true;
                Submit = true;
                this.Dispose();
            }
            
        }

        private void GetCardType()
        {
            var ds = cardTypeHelper.GetList(" Type = '车辆证件'");
            cmbCardType.Items.Clear();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    cmbCardType.Items.Add(new SelectItem(1)
                    {
                        Tag = row["ID"],
                        Text = row["Name"]?.ToString()
                    });
                }
            }
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
